import 'package:flutter/material.dart';
import 'package:my_flutter_app/slide_transition_x.dart';

class AnimatedSwitcherTestIndexRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AnimatedSwitcherTestIndexRoute"),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: <Widget>[
              RaisedButton(
                child: Text("AnimatedSwitcherCounterTestRoute"),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return AnimatedSwitcherCounterTestRoute();
                    }),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class AnimatedSwitcherCounterTestRoute extends StatefulWidget {
  const AnimatedSwitcherCounterTestRoute({Key key}) : super(key: key);

  @override
  _AnimatedSwitcherCounterTestRouteState createState() =>
      _AnimatedSwitcherCounterTestRouteState();
}

class _AnimatedSwitcherCounterTestRouteState
    extends State<AnimatedSwitcherCounterTestRoute> {
  int _count = 0;
  int _count1 = 0;
  int _count2 = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedSwitcherCounterTestRoute'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            AnimatedSwitcher(
              duration: const Duration(milliseconds: 500),
              transitionBuilder: (Widget child, Animation<double> anim) {
                return ScaleTransition(child: child, scale: anim);
              },
              child: Text(
                '$_count',
                // 显示指定key，不同的key会被认为是不同的Text，这样才能执行动画
                key: ValueKey<int>(_count),
                style: Theme.of(context).textTheme.headline,
              ),
            ),
            RaisedButton(
              child: Text('缩放'),
              onPressed: () {
                setState(() {
                  _count++;
                });
              },
            ),

            AnimatedSwitcher(
              duration: const Duration(milliseconds: 500),
              transitionBuilder: (Widget child, Animation<double> anim) {
                var tween = Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0));
                return MySlideTransition(
                    position: tween.animate(anim), child: child);
              },
              child: Text(
                '$_count1',
                // 显示指定key，不同的key会被认为是不同的Text，这样才能执行动画
                key: ValueKey<int>(_count1),
                style: Theme.of(context).textTheme.headline,
              ),
            ),
            RaisedButton(
              child: Text('右入左出'),
              onPressed: () {
                setState(() {
                  _count1++;
                });
              },
            ),

            AnimatedSwitcher(
              duration: const Duration(milliseconds: 500),
              transitionBuilder: (Widget child, Animation<double> anim) {
                return SlideTransitionX(
                    direction: AxisDirection.down, position: anim, child: child);
              },
              child: Text(
                '$_count2',
                // 显示指定key，不同的key会被认为是不同的Text，这样才能执行动画
                key: ValueKey<int>(_count2),
                style: Theme.of(context).textTheme.headline,
              ),
            ),
            RaisedButton(
              child: Text('自定方向'),
              onPressed: () {
                setState(() {
                  _count2++;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

class MySlideTransition extends AnimatedWidget {
  MySlideTransition({
    Key key,
    @required Animation<Offset> position,
    this.transformHitTests = true,
    this.child,
  })  : assert(position != null),
        super(key: key, listenable: position);

  Animation<Offset> get position => listenable;
  final bool transformHitTests;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    Offset offset = position.value;
    if (position.status == AnimationStatus.reverse) {
      offset = Offset(-offset.dx, offset.dy);
    }
    return FractionalTranslation(
      translation: offset,
      transformHitTests: transformHitTests,
      child: child,
    );
  }
}
